﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business.SalePurchase.Sale;
using PJ.Framework.DynamicsAX;
using Microsoft.Dynamics.BusinessConnectorNet;
using PJ.Business.SalePurchase;

namespace PJ.Operation.SalePurchase.Sale
{

    public class AxCreateSaleInvoice : AxCreateInvoice, ICreateSaleInvoice
    {
        #region Interface implementation


        public override ICreateInvoiceResult Execute(string partnerId, string reference, List<string> inventoryItemIds, List<double> quantities, List<string> units)
        {
            using (AxaptaContainer itemIdContainer = DynamicsAXBroker.CreateContainer())
            {
                itemIdContainer.AddRange(inventoryItemIds);
                using (AxaptaContainer quantityContainer = DynamicsAXBroker.CreateContainer())
                {
                    quantityContainer.AddRange(quantities);
                    using (AxaptaContainer unitContainer = DynamicsAXBroker.CreateContainer())
                    {
                        unitContainer.AddRange(units);
                        string directory = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                        directory += "\\AxPdfDocument\\";
                        using (AxaptaContainer container = (AxaptaContainer)DynamicsAXBroker.StaticMethod
                                    ("PJKSaleInvoiceAXProvider", "PostInvoice",
                                    new object[] { directory, partnerId, reference, itemIdContainer, quantityContainer, unitContainer }))
                        {
                            if (container.Count == 2)
                            {
                                string invoiceId = (string)container.get_Item(1);
                                if (invoiceId != null && invoiceId.Length != 0)
                                {
                                    string fileName = directory + invoiceId + ".pdf";
                                    return new CreateInvoiceResult()
                                        {
                                            PdfDocument = System.IO.File.ReadAllBytes(fileName),
                                            Id = invoiceId,
                                            TotalAmount = (double)container.get_Item(2)
                                        };
                                }
                            }
                            throw new AxaptaError("Call of PJKSaleInvoiceAXProvider::PostInvoice returns an invalid result. Parameters : Directory="
                                + directory + " PartnerId=" + partnerId + " Reference=" + reference + " InventoryItemIds=" + inventoryItemIds.ToString()
                                + " Quantities=" + quantities.ToString() + " Unit ids=" + units);
                        }
                    }
                }
            }
        }
        #endregion

    }
}
